package br.com.manutencaocarro.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import br.com.manutencaocarro.R;
import br.com.manutencaocarro.dao.CarroDAO;
import br.com.manutencaocarro.helper.RelatorioHelper;
import br.com.manutencaocarro.modelo.Carro;

public class RelatorioActivity extends Activity 
{
	private RelatorioHelper helper;
	private Spinner lista;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView( R.layout.report );
		helper = new RelatorioHelper(this);
		lista = (Spinner) findViewById(R.id.lista_carros_rel);
		Button botao = (Button) findViewById(R.id.gerarRelatorio);
		
		botao.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				if( helper.validaRelatorio() )
				{
					Intent irParaFormulario = new Intent( RelatorioActivity.this, GeraRelatorioActivity.class );
					irParaFormulario.putExtra( getString(R.string.dadosrelatorio), helper.pegaDadosRelatorio() );
					startActivity(irParaFormulario);
				} else {
					Toast.makeText( RelatorioActivity.this, R.string.validacao_datas, Toast.LENGTH_LONG ).show();
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		carregaLista();
	}
	
	private void carregaLista() 
	{
		CarroDAO dao = new CarroDAO(this);
		List<Carro>  carros = dao.getLista();
		dao.close();

		int layout = android.R.layout.simple_list_item_1;
		ArrayAdapter<Carro> adapter = new ArrayAdapter<>(this, layout , carros);

		lista.setAdapter(adapter);
	}
}